#!/bin/bash                                                        

function enable_ip_forward()
{
  echo 1 > /proc/sys/net/ipv4/ip_forward                              
  sysctl -w net.ipv4.ip_forward=1 1>/dev/null 2>&1

  return
}

function init_iptables_settings()
{
  iptables -F
  iptables -X
  iptables -Z
  iptables -F -t nat
  iptables -X -t nat
  iptables -Z -t nat
  iptables -P INPUT   ACCEPT
  iptables -P OUTPUT  ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -t nat -P PREROUTING   ACCEPT
  iptables -t nat -P POSTROUTING  ACCEPT
  iptables -t nat -P OUTPUT       ACCEPT

  return
}

function load_modules_needed()
{
  modprobe ip_tables 2> /dev/null
  modprobe ip_nat_ftp 2> /dev/null
  modprobe ip_nat_irc 2> /dev/null
  modprobe ip_conntrack 2> /dev/null
  modprobe ip_conntrack_ftp 2> /dev/null
  modprobe ip_conntrack_irc 2> /dev/null

  return
}


function save_iptables_settings()
{
  sleep 3
  iptables-save > `pwd`/iptables.up.rules
# append the last line to /etc/network/interfaces
# auto eth0
# iface eth0 inet dhcp
# pre-up iptables-restore < /pbfd/scripts/network/iptables.up.rules
  CUR_DIR=`pwd`
  cd "`dirname $0`"
  setup_nat="`pwd`/`basename $0` $1 no_save"
  cd "$CUR_DIR"

  SYS_CONF="/etc/init.d/boot.local"
  TMP_CONF="./.boot_local"
  if [ -f "$SYS_CONF" ]
  then
    # For SuSE systems
    cat "$SYS_CONF" | grep -v "`basename $0`" > "$TMP_CONF"
    cat "$TMP_CONF" > "$SYS_CONF"
    echo "$setup_nat" >> "$SYS_CONF"
    rm -f "$TMP_CONF"
  fi

  SYS_CONF="/etc/rc.local"
  TMP_CONF="./.rc_local"
  if [ -f "$SYS_CONF" ]
  then
    # For RHEL & Ubuntu systems
    cat "$SYS_CONF" | grep -v "`basename $0`" > "$TMP_CONF"
    cat "$TMP_CONF" | awk '{if ($0 ~ /^exit.*[0-9]$/) print "'"$setup_nat"'"; print $0}' > "$SYS_CONF"
    rm -f "$TMP_CONF"
  fi

  return
}


function get_remote_addr()
{
  ifconfig "$1" | grep "P-t-P" | awk -F":" '{print $3}' | awk '{print $1}'
}

function get_oslo_itf()
{
  oslo_ip="$1"
  ppp_itf=""
  for i in {0..9}
  do
    remote_ip=`get_remote_addr "ppp$i" 2>/dev/null`
    if [ "$remote_ip" != "" ]
    then
      if [ "$remote_ip" == "$oslo_ip" ]
      then
        ppp_itf="ppp$i"
        echo "$ppp_itf"
        return
      fi
    fi
  done
  return
}

function set_ppp_route()
{
  ppp_itf="$1"
  ppp_addr=`get_remote_addr $ppp_itf`
  [ "$ppp_addr" == "" ]  && return 1
  route del -net 10.0.0.0 netmask 255.0.0.0 1>/dev/null  2>&1
  route add -net 10.0.0.0 netmask 255.0.0.0 gw $ppp_addr device $ppp_itf
}

# -- Main ---
#enable_ip_forward
init_iptables_settings
load_modules_needed

# OSLO client must connect to the server with  $oslo_client_ppp allocated
# See /etc/ppp/chap-secrets for "m11yagent" column
oslo_remote_ip="192.168.11.100"
oslo_itf="`get_oslo_itf $oslo_remote_ip`"
ubp_itf="eth1:0"
hp_itf="eth1"
ubp_net="192.168.0.0/16"
vpn_net="192.168.0.0/16"
hp_net="16.190.0.0/16"
oslo_net="10.0.0.0/8"

conf_type="$1"
if [ "$conf_type" == "client" ]
then
  echo "Configure for LANCE VPN Client"
  iptables -s "$vpn_net" -d "0.0.0.0/0" -o eth0 -t nat -A POSTROUTING -j MASQUERADE
else
  [ "$conf_type" == "" ] && conf_type="server"
  [ "$conf_type" == "fixppp" ] && echo "Fix Current PPP link [$act_ppp]"
  [ "$conf_type" == "server" ] && echo "Configure for LANCE VPN Server"
  [ "$oslo_itf" == "" ] && echo "NO Active PPP linke now" && exit 0


#  iptables -s $my_net -t nat -A PREROUTING -i $my_itf -p tcp --dport 80 -j REDIRECT --to-port 3128
#  iptables -s $hp_net -t nat -A PREROUTING -i $hp_itf -p tcp --dport 80 -j REDIRECT --to-port 3128
  iptables -s $ubp_net -d $oslo_net -o $oslo_itf -t nat -A POSTROUTING -j MASQUERADE
  iptables -s $hp_net -d $oslo_net -o $oslo_itf -t nat -A POSTROUTING -j MASQUERADE
#  iptables -i $hp_itf -p tcp --dport 80 -j REDIRECT --to-port 3128 -t nat -A PREROUTING
  iptables -s $ubp_net -o $hp_itf -t nat -A POSTROUTING -j MASQUERADE

  set_ppp_route "$oslo_itf"

fi


